package com.banana.simpleproject.controller;

import com.banana.simpleproject.dao.IReviewDao;
import com.banana.simpleproject.entity.ReviewBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ReviewController {

    @Resource
    private IReviewDao reviewDao;

    @GetMapping("/reviews")
    public Page<ReviewBean> getAllReview(Integer page){
        return reviewDao.findAll(PageRequest.of(page-1,20));
    }

    @GetMapping("/reviews/{id}")
    public ReviewBean getOneReview(@PathVariable Integer id){
        return reviewDao.findById(id).get();
    }

    @PostMapping("/reviews")
    public ReviewBean addNewReview(ReviewBean reviewBean){
        reviewDao.save(reviewBean);
        return reviewBean;
    }


}
